<template>
	<view class="container">
		<navbar title="确认订单" color='#fff' :background='background' :bgSize='bgSize'></navbar>
		<view class="linear-bg">
			<image :src='hostUrl+"/static/images/sure-order-bg.png"'></image>
		</view>
		<view class="loading-page" v-if='loadingPage'>
			<load-page></load-page>
		</view>
		<view class="wrap" v-else>
			<view v-if='!isEmptyAddress' @tap='goAddress(1)'>
				<address-con :addressObj='addressInfo'></address-con>
			</view>
			<view class="no-address-wrap line-center" v-else @tap='goAddress(1)'>
				<view class="add">
					<uni-icons type='plus-filled' color="#52B148" size="60"></uni-icons>
				</view>
				<view class="title">选择地址</view>
				<image src='/static/line.png' class="address-bg"></image>
			</view>
			<view class="order-goods-wrap">
				<order-goods-list :list='orderDetail.goodsList'></order-goods-list>
			</view>
			<orderInfo :orderDetail="orderDetail" @selectCoupon='selectCoupon'></orderInfo>
		</view>
		<view class="footer-wrap">
			<view class="footer">
				<view class="footer-left">
					<view class="footer-left-price"><text>￥</text>{{orderDetail.orderPayPrice}}</view>
					<view class="footer-left-fee">含运费:¥{{orderDetail.expressPrice}}</view>
				</view>
				<button class="submit-btn line-center" @tap='submitOrder' :disabled="isDisabled"
					:loading="isLoading">提交订单</button>
			</view>
		</view>
		<select-coupon-modal :showModal='showModal' :list='orderDetail.couponList' @close='close' @switchCoupon='getCheckCoupon'></select-coupon-modal>
	</view>
</template>

<script>
	const app = getApp();
	let options;
	import addressCon from '@/components/order/address.vue';
	import orderGoodsList from '@/components/order/order-goods-list.vue';
	import orderInfo from '@/components/order/order-info.vue';
	import selectCouponModal from '../components/select-coupon-modal/select-coupon-modal.vue';
	export default {
		components: {
			addressCon,
			orderGoodsList,
			orderInfo,
			selectCouponModal
		},
		data() {
			return {
				background: `url(${app.globalData.hostUrl}/static/images/sure-order-bg.png)`,
				bgSize: '100% 400rpx',
				hostUrl: app.globalData.hostUrl,
				mode:'',
				couponId:0,
				shareId:0,
				buyNowData: {
					mode: 'buyNow',
					goodsId: '',
					goodsNum: '',
					goodsSkuId: '',
				},
				cartData:{
					mode: 'cart',
					cartIds:'',
				},
				orderDetail: {},
				deepOrderDetail:{},
				loadingPage: true,
				addressInfo: '',
				isEmptyAddress: false,
				isDisabled: false,
				isLoading: false,
				showModal:false,
			};
		},
		watch: {
			addressInfo(newVal, oldVal) {
				const that = this;
				that.addressInfo = newVal;
				that.isEmptyAddress = that.isEmptyObject(that.addressInfo);
			}
		},
		onLoad(option) {
			options = option;
			
		},
		onShow() {
			this.mode = options.mode ? options.mode : 'buyNow';
			this.shareId = app.globalData.shareUid  ? app.globalData.shareUid : 0;
			if(this.mode == 'buyNow'){
				this.buyNowData.goodsId = options.goodsId ? options.goodsId : '';
				this.buyNowData.goodsNum = options.goodsNum ? options.goodsNum : '';
				this.buyNowData.goodsSkuId = options.goodsSkuId ? options.goodsSkuId : '';
			}else{
				this.cartData.cartIds = options.cart_ids ? options.cart_ids : '';
			}
			this.getOrderInfo()
			console.log('this.shareId',this.shareId)
		},
		methods: {
			getOrderInfo() {
				const that = this;
				let postData = {
					couponId:this.couponId,
					share_id:this.shareId
				}
				if(this.mode == 'buyNow'){
					postData = {...this.buyNowData,...postData}
				}else{
					postData = {...this.cartData,...postData}
				}
				console.log('postData',postData)
				app.globalRequest({
					api: app.getApi().order.preOrder,
					data:postData,
				}).then(res => {
					that.loadingPage = false
					if (res.code == 0) {
						that.addressInfo = res.data.address ? res.data.address : {};
						that.isEmptyAddress = that.isEmptyObject(that.addressInfo);
						console.log('isEmptyAddress', that.isEmptyAddress)
						res.data.styles_num = res.data.goodsList.length;
						res.data.couponList.forEach(v=>{
							v.reduce_price_int = parseInt(v.reduce_price);
							v.is_flag = false;
							v.is_checked = false;
						})
						that.orderDetail = res.data;
						that.deepOrderDetail = that.deepClone(res.data);
					}
				})
			},
			goAddress(type) {
				let path;
				path = `/pages/user-page/address-list/address-list?fromS=submit`;
				uni.navigateTo({
					url: path
				})
			},
			isEmptyObject(obj) {
				for (var key in obj) {
					if (obj.hasOwnProperty(key)) {
						return false;
					}
				}
				return true;
			},
			deepClone(obj) {
			    let _tmp = JSON.stringify(obj);//将对象转换为json字符串形式
			    let result = JSON.parse(_tmp);//将转换而来的字符串转换为原生js对象
			    return result;
			},
			selectCoupon(){
				this.showModal = true;
			},
			getCheckCoupon(obj){
				this.couponId = obj.id
				// this.orderDetail.orderPayPrice = this.deepOrderDetail.orderPayPrice;
				// this.orderDetail.couponId = obj.id;
				
				// this.orderDetail.orderPayPrice = (this.orderDetail.orderPayPrice - this.orderDetail.checkCoupon.reduce_price).toFixed(2)
				// console.log('this.orderDetail.orderPayPrice',this.orderDetail.orderPayPrice)
				this.getOrderInfo()
				this.showModal = false;
			},
			close(){
				this.showModal = false;
			},
			submitOrder() {
				const that = this;
				if (that.isEmptyAddress) {
					uni.showToast({
						title: '请添加地址',
						icon: 'none'
					})
					return;
				}
				let postData = {
					couponId:this.orderDetail.couponId,
					share_id:this.shareId,
					address_id: that.addressInfo.address_id,
					remark: ''
				}
				if(this.mode == 'buyNow'){
					postData = {...this.buyNowData,...postData}
				}else{
					postData = {...this.cartData,...postData}
				}
				console.log('postData',postData)
				that.isDisabled = true;
				that.isLoading = true;
				app.globalRequest({
					api: app.getApi().order.orderSubmit,
					method: 'POST',
					data: postData
				}).then(res => {
					if (res.code == 0) {
						if(res.data.payment){
							const payment = res.data.payment
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: `${payment.timestamp}`,
								nonceStr: payment.nonceStr,
								package: payment.package,
								signType: payment.signType,
								paySign: payment.paySign,
								success: function(res) {
									uni.showToast({
										title: '支付成功',
										icon: 'none'
									})
									setTimeout(() => {
										uni.redirectTo({
											url: '/pages/user-page/my-order/my-order?type=delivery'
										})
										that.isDisabled = false;
										that.isLoading = false;
									}, 1000)
								},
								fail: function(err) {
									uni.showToast({
										title: '支付失败',
										icon: 'none'
									})
									that.isDisabled = false;
									that.isLoading = false;
								}
							})
						}else{
							uni.showToast({
								title: '支付成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/user-page/my-order/my-order?type=delivery'
								})
								that.isDisabled = false;
								that.isLoading = false;
							}, 1000)
						}
						
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						that.isDisabled = false;
						that.isLoading = false;
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.linear-bg {
		width: 100%;
		height: 400rpx;
		position: absolute;
		left: 0;
		top: 0;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.wrap {
		position: relative;
		z-index: 2;
		padding-bottom: 200rpx;
	}

	.address-bg {
		width: 712rpx;
		height: 8rpx;
		position: absolute;
		left: -5rpx;
		bottom: 0;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.order-goods-wrap {
		margin: 24rpx 24rpx 0;
		padding: 24rpx;
		background: #fff;
		border-radius: 16rpx;
	}

	.no-address-wrap {
		margin: 24rpx 24rpx 0;
		flex-direction: column;
		background: #fff;
		height: 200rpx;
		border-radius: 16rpx 16rpx 0 0;
		position: relative;
	}

	.footer-wrap {
		width: 100%;
		height: 120rpx;
		background: #FFFFFF;
		box-shadow: inset 0rpx 1rpx 0rpx 2rpx rgba(0, 0, 0, 0.1);
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 22;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		box-sizing: content-box;

		.footer {
			width: 100%;
			height: 100%;
			padding: 0 24rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.footer-left-price {
				color: #FF5040;
				font-size: 48rpx;

				text {
					font-size: 28rpx;
				}
			}

			.footer-left-fee {
				font-size: 24rpx;
				color: #AFB0B2;
			}

			.submit-btn {
				width: 220rpx;
				height: 88rpx;
				background: $supplier-btn-linear-color;
				border-radius: 44rpx;
				color: #fff;
				font-size: 32rpx;
				margin-left: 0;
				margin-right: 0;
			}
		}
	}
</style>